package com.xbongbong.workflow.service.strategy.data.impl;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.enums.FlowStatusEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.enums.TaskTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.DateTimeUtil;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.paas.util.ExplainUtil;
import com.xbongbong.paas.util.FormDataUtil;
import com.xbongbong.pro.domain.entity.UpdateDataEntity;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.pro.invoice.pojo.InvoiceCommonSavePojo;
import com.xbongbong.pro.invoice.pojo.InvoiceRelationshipEditPojo;
import com.xbongbong.pro.workflow.enums.WorkflowOperateEnum;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.PurchaseInvoiceRelationshipEntity;
import com.xbongbong.saas.domain.entity.ext.PurchaseInvoiceEntityExt;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.PurchaseInvoiceEnum;
import com.xbongbong.saas.enums.business.RefTypeEnum;
import com.xbongbong.saas.help.PurchaseInvoiceHelp;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.saas.model.PurchaseInvoiceModel;
import com.xbongbong.saas.model.PurchaseInvoiceRelationshipModel;
import com.xbongbong.saas.service.PurchaseInvoiceService;
import com.xbongbong.saas.toolbox.help.FastJsonHelper;
import com.xbongbong.sys.domain.entity.UserEntity;
import com.xbongbong.sys.model.UserModel;
import com.xbongbong.workflow.domain.entity.WorkflowBackupDataEntity;
import com.xbongbong.workflow.model.WorkflowBackupDataModel;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.User;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.service.strategy.data.AbstractWorkflowDataStrategy;
import com.xbongbong.workflow.service.strategy.data.WorkflowDataProcessStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import static com.xbongbong.saas.toolbox.help.FastJsonHelper.getDoubleOrDefaultFromFormData;

/**
 *
 * @author 魏荣杰
 *
 */
@Service
@Slf4j
public class WorkflowPurchaseInvoiceStrategy extends AbstractWorkflowDataStrategy implements WorkflowDataProcessStrategy {

    @Resource
    private PurchaseInvoiceModel purchaseInvoiceModel;
    @Resource
    private WorkflowBackupDataModel workflowBackupDataModel;
    @Resource
    private PurchaseInvoiceRelationshipModel purchaseInvoiceRelationshipModel;
    @Resource
    private UserModel userModel;
    @Resource
    private PurchaseInvoiceService purchaseInvoiceService;
    @Resource
    private PurchaseInvoiceHelp purchaseInvoiceHelp;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;

    @Override
    public List<Integer> type() {
        return Collections.singletonList(XbbRefTypeEnum.PURCHASE_INVOICE.getCode());
    }

    @Override
    public void save(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        // 文件柜
        saveDataCabinetFile(workflowTransferPOJO);

        User user = workflowTransferPOJO.getUser();
        String corpid = workflowTransferPOJO.getCorpid();
        Integer isImport = workflowTransferPOJO.getIsImport();
        Data pojoData = workflowTransferPOJO.getData();
        Long dataId = pojoData.getId();
        JSONObject data = pojoData.getData();

        boolean isNew = true;
        boolean isRed = false;
        if(data.containsKey(PurchaseInvoiceEnum.ORIGIN_ID.getAttr())){
            Long originId = FastJsonHelper.getLongOrDefaultFromFormData(data,PurchaseInvoiceEnum.ORIGIN_ID.getAttr(),0L);
            if(originId > 0){
                isRed = true;
            }
        }
        boolean isEnable = false;
        Long originId = FastJsonHelper.getLongOrDefaultFromFormData(data,PurchaseInvoiceEnum.ORIGIN_ID.getAttr(),0L);
        if (originId > BasicConstant.ZERO) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(PurchaseInvoiceEnum.HAS_RED.getAttr(), BasicConstant.ONE);
            UpdateDataEntity updateData = ExplainUtil.getUpdateData(originId, jsonObject, corpid);
            purchaseInvoiceModel.updateBatch(Collections.singletonList(updateData), corpid);
            jsonObject = new JSONObject();
            jsonObject.put(PurchaseInvoiceEnum.IS_RED.getAttr(), BasicConstant.ONE);
            UpdateDataEntity updateData1 = ExplainUtil.getUpdateData(dataId, jsonObject, corpid);
            purchaseInvoiceModel.updateBatch(Collections.singletonList(updateData1), corpid);
        }
        if (originId > BasicConstant.ZERO) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put(PurchaseInvoiceEnum.HAS_RED.getAttr(), BasicConstant.ONE);
            UpdateDataEntity updateData = ExplainUtil.getUpdateData(originId, jsonObject, corpid);
            purchaseInvoiceModel.updateBatch(Collections.singletonList(updateData), corpid);
            jsonObject = new JSONObject();
            jsonObject.put(PurchaseInvoiceEnum.IS_RED.getAttr(), BasicConstant.ONE);
            UpdateDataEntity updateData1 = ExplainUtil.getUpdateData(dataId, jsonObject, corpid);
            purchaseInvoiceModel.updateBatch(Collections.singletonList(updateData1), corpid);
        }

        Object purchaseNoObject = data.get(PurchaseInvoiceEnum.PURCHASE_ID_LINK_TEXT.getAttr());
        List<String> purchaseNoList = null;
        if(Objects.nonNull(purchaseNoObject)){
            if(purchaseNoObject instanceof String[]){
                String[]  purchaseNos = (String[]) purchaseNoObject;
                purchaseNoList = new ArrayList<>(Arrays.asList(purchaseNos));
            }else if(purchaseNoObject instanceof List){
                purchaseNoList = (List<String>) purchaseNoObject;
            }
        }else {
            purchaseNoList = new ArrayList<>();
        }

        InvoiceRelationshipEditPojo invoiceRelationshipEditPojo = purchaseInvoiceHelp.getAddOrDeleteRelationship(data, data, corpid, dataId);
        //开票金额
        Double totalMoney = getDoubleOrDefaultFromFormData(data, PurchaseInvoiceEnum.TOTAL_MONEY.getAttr(),0D);
        Long now = (long) DateUtil.getInt();
        Long supplierId = FastJsonHelper.getLongOrDefaultFromFormData(data, PurchaseInvoiceEnum.SUPPLIER_NAME.getAttr(), 0L);
        String supplierName = FastJsonHelper.getStringOrDefaultFromFormData(data,PurchaseInvoiceEnum.SUPPLIER_NAME_LINK_TEXT.getAttr(), "");
//        if(!isNew){
//            // 删除原先的的合同和回款关系，以后再改成批量更新方式
//            purchaseInvoiceRelationshipModel.deleteByInvoiceId(dataId, corpid);
//        }

        PurchaseInvoiceEntityExt purchaseInvoiceEntityExt = purchaseInvoiceModel.getByKey(dataId, corpid);
        UserEntity userEntity = userModel.getByKeyIngoreDel(user.getUserId(), corpid);
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(userEntity, userVO);

        InvoiceCommonSavePojo invoiceCommonSavePojo = new InvoiceCommonSavePojo(invoiceRelationshipEditPojo.getPurchaseIdList(), corpid, dataId, originId, now, saasNeedRedundantAttrPojo, isNew, isEnable, isImport, isRed, totalMoney,saasNeedRedundantAttrPojo.getAmountDetail(), purchaseInvoiceEntityExt, userVO, pojoData.getBusinessType(), pojoData.getSubBusinessType());
        boolean purchaseInvoiceFlag = saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPayPlan() && !saasNeedRedundantAttrPojo.isHasPaySheet();
        boolean payPlanInvoiceFlag = saasNeedRedundantAttrPojo.isHasPayPlan() && saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPaySheet();
        boolean paySheetInvoiceFlag = saasNeedRedundantAttrPojo.isHasPaySheet() && saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPayPlan();
        boolean isPrePaySheetFlag = saasNeedRedundantAttrPojo.isPrePaySheet();
//        try {
        try {
            String lang = LocaleContextHolder.getLocale().toString();
            if(saasNeedRedundantAttrPojo.isPurchaseUnion() || purchaseInvoiceFlag) {
                //合同不为空 其他为空 合同合并开票
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPurchaseIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PURCHASE);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else if(saasNeedRedundantAttrPojo.isPayPlanUnion() || payPlanInvoiceFlag) {
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPayPlanIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PAYPLAN);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else if(saasNeedRedundantAttrPojo.isPaySheetUnion() || paySheetInvoiceFlag || isPrePaySheetFlag) {
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPaySheetIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PAYSHEET);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else {
                PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity = purchaseInvoiceHelp.buildNewInvoiceRelation(false, dataId, now, supplierId, supplierName, corpid,totalMoney,0L,"",0L, "",0L, "",0);
                if (Objects.nonNull(purchaseInvoiceRelationshipEntity)) {
                    try {
                        purchaseInvoiceRelationshipModel.insert(purchaseInvoiceRelationshipEntity);
                    } catch (Exception e) {
                        log.error("插入发票关系实体表出错", e);
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
                    }
                }
                //消息推送
                purchaseInvoiceHelp.purchaseInvoiceMessagePushHandle(invoiceCommonSavePojo);
            }
        } catch (Exception e) {
            log.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowPurchaseInvoiceStrategy.save", e);
        }
//        } catch (XbbException e) {
//            rollBack4LinkEs(null, corpid, invoiceRelationshipEditPojo.getContractIdList(), invoiceRelationshipEditPojo.getPaymentIdList(), invoiceRelationshipEditPojo.getPaymentSheetIdList());
//            LOG.error("PurchaseInvoiceServiceImpl.afterSave 执行出错：", e);
//            throw e;
//        }catch (Exception e){
//            rollBack4LinkEs(null, corpid, invoiceRelationshipEditPojo.getContractIdList(), invoiceRelationshipEditPojo.getPaymentIdList(), invoiceRelationshipEditPojo.getPaymentSheetIdList());
//            LOG.error("PurchaseInvoiceServiceImpl.afterSave 执行出错：", e);
//            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
//        }


        // api
        apiHook(workflowTransferPOJO);
        addLinkDataDynamic(workflowTransferPOJO);
        // log
        afterSaveLog(workflowTransferPOJO);
    }

    @Override
    public void update(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        // 文件柜
        saveDataCabinetFile(workflowTransferPOJO);

        User user = workflowTransferPOJO.getUser();
        String corpid = workflowTransferPOJO.getCorpid();
        Integer isImport = workflowTransferPOJO.getIsImport();
        Data pojoData = workflowTransferPOJO.getData();
        Long dataId = pojoData.getId();
        JSONObject data = pojoData.getData();

        boolean isNew = false;
        boolean isRed = false;
        if(data.containsKey(PurchaseInvoiceEnum.ORIGIN_ID.getAttr())){
            Long originId = FastJsonHelper.getLongOrDefaultFromFormData(data,PurchaseInvoiceEnum.ORIGIN_ID.getAttr(),0L);
            if(originId > 0){
                isRed = true;
            }
        }
        boolean isEnable = false;
        Long originId = FastJsonHelper.getLongOrDefaultFromFormData(data,PurchaseInvoiceEnum.ORIGIN_ID.getAttr(),0L);

        Object purchaseNoObject = data.get(PurchaseInvoiceEnum.PURCHASE_ID_LINK_TEXT.getAttr());
        List<String> purchaseNoList = null;
        if(Objects.nonNull(purchaseNoObject)){
            if(purchaseNoObject instanceof String[]){
                String[]  purchaseNos = (String[]) purchaseNoObject;
                purchaseNoList = new ArrayList<>(Arrays.asList(purchaseNos));
            }else if(purchaseNoObject instanceof List){
                purchaseNoList = (List<String>) purchaseNoObject;
            }
        }else {
            purchaseNoList = new ArrayList<>();
        }

        InvoiceRelationshipEditPojo invoiceRelationshipEditPojo = purchaseInvoiceHelp.getAddOrDeleteRelationship(data, data, corpid, dataId);
        //开票金额
        Double totalMoney = getDoubleOrDefaultFromFormData(data, PurchaseInvoiceEnum.TOTAL_MONEY.getAttr(),0D);
        Long now = (long) DateUtil.getInt();
        Long supplierId = FastJsonHelper.getLongOrDefaultFromFormData(data, PurchaseInvoiceEnum.SUPPLIER_NAME.getAttr(), 0L);
        String supplierName = FastJsonHelper.getStringOrDefaultFromFormData(data,PurchaseInvoiceEnum.SUPPLIER_NAME_LINK_TEXT.getAttr(), "");

        // 删除原先的的合同和回款关系，以后再改成批量更新方式
        purchaseInvoiceRelationshipModel.deleteByInvoiceId(dataId, corpid);

        PurchaseInvoiceEntityExt purchaseInvoiceEntityExt = purchaseInvoiceModel.getByKey(dataId, corpid);
        UserEntity userEntity = userModel.getByKeyIngoreDel(user.getUserId(), corpid);
        UserVO userVO = new UserVO();
        BeanUtil.copyProperties(userEntity, userVO);

        InvoiceCommonSavePojo invoiceCommonSavePojo = new InvoiceCommonSavePojo(invoiceRelationshipEditPojo.getPurchaseIdList(), corpid, dataId, originId, now, saasNeedRedundantAttrPojo, isNew, isEnable, isImport, isRed, totalMoney,saasNeedRedundantAttrPojo.getAmountDetail(), purchaseInvoiceEntityExt, userVO, pojoData.getBusinessType(), pojoData.getSubBusinessType());
        boolean purchaseInvoiceFlag = saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPayPlan() && !saasNeedRedundantAttrPojo.isHasPaySheet();
        boolean payPlanInvoiceFlag = saasNeedRedundantAttrPojo.isHasPayPlan() && saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPaySheet();
        boolean paySheetInvoiceFlag = saasNeedRedundantAttrPojo.isHasPaySheet() && saasNeedRedundantAttrPojo.isHasPurchase() && !saasNeedRedundantAttrPojo.isHasPayPlan();
        boolean isPrePaySheetFlag = saasNeedRedundantAttrPojo.isPrePaySheet();
//        try {
        try {
            String lang = LocaleContextHolder.getLocale().toString();
            if(saasNeedRedundantAttrPojo.isPurchaseUnion() || purchaseInvoiceFlag) {
                //合同不为空 其他为空 合同合并开票
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPurchaseIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PURCHASE);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else if(saasNeedRedundantAttrPojo.isPayPlanUnion() || payPlanInvoiceFlag) {
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPayPlanIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PAYPLAN);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else if(saasNeedRedundantAttrPojo.isPaySheetUnion() || paySheetInvoiceFlag || isPrePaySheetFlag) {
                invoiceCommonSavePojo.setRefIdList(invoiceRelationshipEditPojo.getPaySheetIdList());
                invoiceCommonSavePojo.setRefTypeEnum(RefTypeEnum.PAYSHEET);
                invoiceCommonSavePojo.setPurchaseNos(purchaseNoList);
                invoiceCommonSavePojo.setSupplierId(supplierId);
                invoiceCommonSavePojo.setSupplierName(supplierName);
                purchaseInvoiceService.invoiceCommonSaveHandle(invoiceCommonSavePojo);
            }else {
                PurchaseInvoiceRelationshipEntity purchaseInvoiceRelationshipEntity = purchaseInvoiceHelp.buildNewInvoiceRelation(false, dataId, now, supplierId, supplierName, corpid,totalMoney,0L,"",0L, "",0L, "",0);
                if (Objects.nonNull(purchaseInvoiceRelationshipEntity)) {
                    try {
                        purchaseInvoiceRelationshipModel.insert(purchaseInvoiceRelationshipEntity);
                    } catch (Exception e) {
                        log.error("插入发票关系实体表出错", e);
                        throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
                    }
                }
                //消息推送
                purchaseInvoiceHelp.purchaseInvoiceMessagePushHandle(invoiceCommonSavePojo);
            }
        } catch (Exception e) {
            log.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowPurchaseInvoiceStrategy.update", e);
        }
//        } catch (XbbException e) {
//            rollBack4LinkEs(null, corpid, invoiceRelationshipEditPojo.getContractIdList(), invoiceRelationshipEditPojo.getPaymentIdList(), invoiceRelationshipEditPojo.getPaymentSheetIdList());
//            LOG.error("PurchaseInvoiceServiceImpl.afterSave 执行出错：", e);
//            throw e;
//        }catch (Exception e){
//            rollBack4LinkEs(null, corpid, invoiceRelationshipEditPojo.getContractIdList(), invoiceRelationshipEditPojo.getPaymentIdList(), invoiceRelationshipEditPojo.getPaymentSheetIdList());
//            LOG.error("PurchaseInvoiceServiceImpl.afterSave 执行出错：", e);
//            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, SystemErrorCodeEnum.API_ERROR_100001.getMsg());
//        }

        // api
        apiHook(workflowTransferPOJO);
        // log
        afterUpdateLog(workflowTransferPOJO);

    }

    @Override
    public void updateBack(Long taskId, WorkflowTransferPOJO workflowTransferPOJO) throws XbbException {
        try {
            String corpid = workflowTransferPOJO.getCorpid();
            Long backupDataId = workflowTransferPOJO.getBackupDataId();
            Long dataId = workflowTransferPOJO.getData().getId();
            WorkflowBackupDataEntity workflowBackupDataEntity = workflowBackupDataModel.getByKey(backupDataId, corpid);
            if (Objects.nonNull(workflowBackupDataEntity)) {
                JSONObject data = workflowBackupDataEntity.getData();

                replenishAttrForBackupData(workflowTransferPOJO.getData().getData(), data);

                PurchaseInvoiceEntityExt purchaseInvoiceEntityExt = new PurchaseInvoiceEntityExt();
                purchaseInvoiceEntityExt.setId(dataId);
                purchaseInvoiceEntityExt.setCorpid(corpid);
                purchaseInvoiceEntityExt.setFormId(workflowTransferPOJO.getData().getFormId());
                purchaseInvoiceEntityExt.setData(data);
                purchaseInvoiceEntityExt.setSerialNo(data.getString(FieldTypeEnum.SERIALNO.getAlias()));
                // 获取负责人
                purchaseInvoiceEntityExt.setOwnerId(getOwnerId(data));
                purchaseInvoiceEntityExt.setDepartmentId(data.getLong(FieldTypeEnum.DEPARTMENTID.getAlias()));

                FormDataUtil.removeSystemData(data);
                if (Objects.equals(workflowTransferPOJO.getOperate(), WorkflowOperateEnum.UPDATE.getType())) {
                    purchaseInvoiceEntityExt.setFlowStatus(FlowStatusEnum.PASS.getType());
                }
                purchaseInvoiceModel.update(purchaseInvoiceEntityExt);

                // 回退被删掉的子表单
                updateBackSubForm(purchaseInvoiceEntityExt, corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_SUB);
                //回退被删掉的富文本
                updateBackRichText(taskId, workflowTransferPOJO);

                updateBackSubForm(purchaseInvoiceEntityExt, corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_EXPRESS);

            }
        } catch (Exception e) {
            log.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowPurchaseInvoiceStrategy.updateBack error ", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public void approvalFailed(Long taskId, WorkflowTransferPOJO workflowTransferPOJO, Integer taskType) throws XbbException {
        try {
            String corpid = workflowTransferPOJO.getCorpid();
            Data workflowData = workflowTransferPOJO.getData();
            Long dataId = workflowData.getId();
            Long formId = workflowData.getFormId();
            Integer businessType = workflowData.getBusinessType();
            Integer saasMark = workflowData.getSaasMark();
            PurchaseInvoiceEntityExt purchaseInvoiceEntityExt = new PurchaseInvoiceEntityExt();
            purchaseInvoiceEntityExt.setId(dataId);
            if (Objects.equals(TaskTypeEnum.REVOKE.getType(), taskType)) {
                purchaseInvoiceEntityExt.setFlowStatus(FlowStatusEnum.REVOKE.getType());
            } else {
                purchaseInvoiceEntityExt.setFlowStatus(FlowStatusEnum.REJECT.getType());
            }
            purchaseInvoiceEntityExt.setCorpid(corpid);
            purchaseInvoiceEntityExt.setUpdateTime(DateTimeUtil.getInt());
            purchaseInvoiceModel.update(purchaseInvoiceEntityExt);
            // 子表单
            DelEnum del = DelEnum.APPROVE_FAILED;

            paasFormSubDataModel.deleteBatchByDataId(Collections.singletonList(dataId), corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_EXPRESS, del);
            paasFormSubDataModel.deleteBatchByDataId(Collections.singletonList(dataId), corpid, IndexTypeEnum.IDX_SAAS_PURCHASE_INVOICE_SUB, del);
            //富文本
            deleteFormRichText(dataId, corpid, formId, businessType, saasMark, DelEnum.NORMAL);

        } catch (Exception e) {
            log.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowPurchaseInvoiceStrategy.approvalFailed error ", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
